package com.xhh.nphm.common.utils.file;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.xml.rpc.ServiceException;
import java.io.*;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

@Component
public class FileTools {

    public static String downloadUrl;

    @Value("${download.backUrl}")
    public String tmpUrl;

    /**
     * 文件夹目录名
     */
    public static String dirName = "backAccessory";

    @PostConstruct
    public void init() {
        downloadUrl= tmpUrl;
    }

    /**
     * 跨域上传图片
     * @param wsCode 流水号
     * @param bytes 图片数组
     * @param fileName
     * @return
     * @throws ServiceException
     * @throws MalformedURLException
     * @throws RemoteException
     */
    public static String savaImg(String wsCode, byte[] bytes, String fileName) throws ServiceException, MalformedURLException, RemoteException {
        Service service=new Service();
        Call call=(Call)service.createCall();
        System.out.print(bytes);
        call.setTargetEndpointAddress(new URL(downloadUrl));
        call.setOperation("FilesUpload"); //TODO 更改了
        call.setMaintainSession(true);
        String path = (String) call.invoke(new Object[]{wsCode,bytes,dirName,fileName});
        return path;
    }

    /**
     * 跨域获取附件名称
     * @param wsCode 流水号
     * @return 附件名称
     * @throws ServiceException
     * @throws MalformedURLException
     * @throws RemoteException
     */
    public static String[] getAccessoryName(String wsCode) throws ServiceException, IOException {
        Service service=new Service();
        Call call=(Call)service.createCall();
        call.setTargetEndpointAddress(new URL(downloadUrl));
        call.setOperationName("getAccessoryName");
        call.setMaintainSession(true);
        Object invoke = call.invoke(new Object[]{wsCode,dirName});
        return castToArray(invoke);
    }

    public static String[] getAccessoryName(String wsCode, String rootDirName) throws ServiceException, IOException {
        Service service=new Service();
        Call call=(Call)service.createCall();
        call.setTargetEndpointAddress(new URL(downloadUrl));
        call.setOperationName("getAccessoryName");
        call.setMaintainSession(true);
        Object invoke = call.invoke(new Object[]{wsCode,rootDirName});
        return castToArray(invoke);
    }


    /**
     * 通过附件名下载附件
     * @param wsCode 流水号
     * @param fileName 附件路径
     * @return 附件
     * @throws ServiceException
     * @throws MalformedURLException
     * @throws RemoteException
     */
    public static byte[] getAccessoryByName(String wsCode,String fileName) throws ServiceException, IOException {
        Service service = new Service();
        Call call=(Call)service.createCall();
        call.setTargetEndpointAddress(new URL(downloadUrl));
        call.setOperation("getAccessoryByName");
        call.setMaintainSession(true);
        return (byte[]) call.invoke(new Object[]{wsCode,dirName,fileName});
    }

    public static byte[] getAccessoryByName(String wsCode,String fileName,String rootDirName) throws ServiceException, IOException {
        Service service=new Service();
        Call call=(Call)service.createCall();
        call.setTargetEndpointAddress(new URL(downloadUrl));
        call.setOperation("getAccessoryByName");
        call.setMaintainSession(true);
        return (byte[]) call.invoke(new Object[]{wsCode,rootDirName,fileName});
    }


    public static String[] castToArray(Object arr) throws IOException{
        int len = Array.getLength(arr);
        String[] strArr = new String[len];
        for (int i = 0; i < len; i++) {
            strArr[i] = (String) Array.get(arr, i);
        }
        return strArr;
    }

    /**
     * 获得指定文件的byte数组
     */
    public static byte[] getBytes(File file){
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
    
    public static void main(String[] args) throws Exception{
        String wsCode = "222101900044";
        String fileName = "C:\\Users\\ThinkPad\\Pictures\\2a56e70351b67273bc5e8ac5a62b1784.jpg";
        File file = new File(fileName);
        downloadUrl = "http://127.0.0.1:8085/savaServer/services/InterMainService2";
        byte[] bytes = FileTools.getBytes(file);//获得图片数组
        FileTools.savaImg(wsCode,bytes,"5b564b0ecdd0865e_-1x-1.jpg");
//        System.out.println(Arrays.toString(FileTools.getAccessoryByName("E:/fileUpLoad/backAccessory/20221021/222102100001/wallhaven-q29vpr.jpg")));
    }
}
